﻿Imports System.Security.Cryptography
Imports System.IO

Public Class LogIn2
    Inherits Login

    Protected Key() As Byte
    Protected IV() As Byte

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Key = UTF8Encoding.UTF8.GetBytes("passwordpassword")
        IV = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}

        UserEmail.Text = getUserName()
        UserPass.Attributes.Add("value", getPassword())


        Session("PageRedirect") = getReportAddress()
       
    End Sub

    Protected Function getUserName() As String
        Return HttpContext.Current.Request.Params("user")
    End Function

    Protected Function getPassword() As String
		'Return HttpContext.Current.Request.Params("pass")
        Dim EncryptedPassword As String = HttpContext.Current.Request.Params("pass")
        Dim PlainPassword As String = Decrypt(EncryptedPassword)
        Return PlainPassword
    End Function

    Public Function getReportAddress() As String

        Return Server.UrlDecode(HttpContext.Current.Request.Params("report"))

        ' Server.UrlDecode("InicioForecas.aspx")
        ' Return Server.UrlDecode(HttpContext.Current.Request.Params("report"))
    End Function

    Public Function Encrypt(ByVal PlainText As String) As String
        Dim PlainBytes() As Byte = UTF8Encoding.UTF8.GetBytes(PlainText)
        Dim AesProvider As AesCryptoServiceProvider = New AesCryptoServiceProvider()
        Dim CryptoTransform As ICryptoTransform = AesProvider.CreateEncryptor(Key, IV)
        Dim MemoryEncryptedStream As MemoryStream = New MemoryStream()
        Dim EncryptedStream As CryptoStream = New CryptoStream(MemoryEncryptedStream, CryptoTransform, CryptoStreamMode.Write)
        EncryptedStream.Write(PlainBytes, 0, PlainBytes.Length)
        EncryptedStream.FlushFinalBlock()
        MemoryEncryptedStream.Position = 0
        Dim EncryptedBytes(MemoryEncryptedStream.Length - 1) As Byte
        MemoryEncryptedStream.Read(EncryptedBytes, 0, MemoryEncryptedStream.Length)
        EncryptedStream.Close()
        MemoryEncryptedStream.Close()
        Return Convert.ToBase64String(EncryptedBytes)
    End Function

    Public Function Decrypt(ByVal EncryptedText As String) As String
        Dim EncryptedBytes As Byte() = Convert.FromBase64String(EncryptedText)
        Dim AesProvider As AesCryptoServiceProvider = New AesCryptoServiceProvider()
        Dim CryptoTransform As ICryptoTransform = AesProvider.CreateDecryptor(Key, IV)
        Dim MemoryDecryptedStream As MemoryStream = New MemoryStream()
        Dim DecryptedStream As CryptoStream = New CryptoStream(MemoryDecryptedStream, CryptoTransform, CryptoStreamMode.Write)
        DecryptedStream.Write(EncryptedBytes, 0, EncryptedBytes.Length)
        DecryptedStream.FlushFinalBlock()
        MemoryDecryptedStream.Position = 0
        Dim PlainBytes(MemoryDecryptedStream.Length - 1) As Byte
        MemoryDecryptedStream.Read(PlainBytes, 0, MemoryDecryptedStream.Length)
        DecryptedStream.Close()
        MemoryDecryptedStream.Close()
        Return UTF8Encoding.UTF8.GetString(PlainBytes)
    End Function
End Class